package com.axend.lib_base.view.timepicker;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.viewbinding.ViewBinding;


import com.axend.lib_base.R;
import com.axend.lib_base.databinding.BaseFragmentTimeBottomDialogBinding;
import com.jzxiang.pickerview.TimeWheel;
import com.jzxiang.pickerview.config.PickerConfig;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.data.WheelCalendar;
import com.jzxiang.pickerview.listener.OnDateSetListener;

public class BottomTimeDialogFragment extends DialogFragment {
    private ViewBinding dialogBinding;

    private TimeWheel mTimeWheel;

    PickerConfig mPickerConfig;

    private static BottomTimeDialogFragment newIntance(PickerConfig pickerConfig) {
        BottomTimeDialogFragment timePickerDialog = new BottomTimeDialogFragment();
        timePickerDialog.initialize(pickerConfig);
        return timePickerDialog;
    }

    private void initialize(PickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }


    public static class Builder {
        PickerConfig mPickerConfig;

        public Builder() {
            mPickerConfig = new PickerConfig();
        }

        public Builder setType(Type type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setThemeColor(int color) {
            mPickerConfig.mThemeColor = color;
            return this;
        }

        public Builder setCancelStringId(String left) {
            mPickerConfig.mCancelString = left;
            return this;
        }

        public Builder setSureStringId(String right) {
            mPickerConfig.mSureString = right;
            return this;
        }

        public Builder setTitleStringId(String title) {
            mPickerConfig.mTitleString = title;
            return this;
        }

        public Builder setToolBarTextColor(int color) {
            mPickerConfig.mToolBarTVColor = color;
            return this;
        }

        public Builder setWheelItemTextNormalColor(int color) {
            mPickerConfig.mWheelTVNormalColor = color;
            return this;
        }

        public Builder setWheelItemTextSelectorColor(int color) {
            mPickerConfig.mWheelTVSelectorColor = color;
            return this;
        }

        public Builder setWheelItemTextSize(int size) {
            mPickerConfig.mWheelTVSize = size;
            return this;
        }

        public Builder setCyclic(boolean cyclic) {
            mPickerConfig.cyclic = cyclic;
            return this;
        }

        public Builder setMinMillseconds(long millseconds) {
            mPickerConfig.mMinCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setMaxMillseconds(long millseconds) {
            mPickerConfig.mMaxCalendar = new WheelCalendar(millseconds);
            return this;
        }

        public Builder setCurrentMillseconds(long millseconds) {
            mPickerConfig.mCurrentCalendar = new WheelCalendar(millseconds);
            if(millseconds < 0){
                //设置默认年份 < 0 calendar  需要手动计算, 此处先写死
                mPickerConfig.mCurrentCalendar.year = 1960;
                mPickerConfig.mCurrentCalendar.month = 1;
                mPickerConfig.mCurrentCalendar.day = 1;
            }

            return this;
        }

        public Builder setYearText(String year){
            mPickerConfig.mYear = year;
            return this;
        }

        public Builder setMonthText(String month){
            mPickerConfig.mMonth = month;
            return this;
        }

        public Builder setDayText(String day){
            mPickerConfig.mDay = day;
            return this;
        }

        public Builder setHourText(String hour){
            mPickerConfig.mHour = hour;
            return this;
        }

        public Builder setMinuteText(String minute){
            mPickerConfig.mMinute = minute;
            return this;
        }

        public Builder setCallBack(OnDateSetListener listener) {
            mPickerConfig.mCallBack = listener;
            return this;
        }

        public BottomTimeDialogFragment build() {
            return newIntance(mPickerConfig);
        }

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //加这句话去掉自带的标题栏
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogBinding = DataBindingUtil.inflate(getLayoutInflater(),
               R.layout.base_fragment_time_bottom_dialog, container, false);
        return dialogBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    void sureClicked() {
        String time = "";
        if(Type.HOURS_MINS == mPickerConfig.mType){
            int hour = mTimeWheel.getCurrentHour();
            int min = mTimeWheel.getCurrentMinute();
            if(min<10){
                time = hour + ":0" + min;
            }else{
                time = hour + ":" + min;
            }

        }else{
            int year = mTimeWheel.getCurrentYear();
            int month = mTimeWheel.getCurrentMonth();
            int day = mTimeWheel.getCurrentDay();
            time = year + "-" + month + "-" + day;
        }


        if (mPickerConfig.mCallBack != null) {
            mPickerConfig.mCallBack.onDateSet(this, time);
        }
        dismiss();
    }


    void initView() {
        mTimeWheel = new TimeWheel(dialogBinding.getRoot(), mPickerConfig);
        if (dialogBinding instanceof BaseFragmentTimeBottomDialogBinding){
            ((BaseFragmentTimeBottomDialogBinding) dialogBinding).roomSure.setOnClickListener(v->sureClicked());
            ((BaseFragmentTimeBottomDialogBinding) dialogBinding).roomCancel.setOnClickListener(v->dismiss());
        }


    }

}
